$(function () {
    layui.use(['table','layer','form'],function () {
        var table = layui.table;
        var layer = layui.layer;
        var form = layui.form
        table.render({
            elem:'#listSplit',
            url:'/pages/back/item/list',
            toolbar: '#toolbarDemo',//开启头部工具栏，并为其绑定左侧模板
            defaultToolbar: ['filter', 'exports', 'print'],
            cols:[[
                {field: 'iid', title: '货物类型编号',sort: true, fixed: 'left'},
                {field: 'title', title: '货物类型名称',sort: true, fixed: 'left'},
                {field: 'note', title: '货物类型描述'},
                {field:'right',title:'操作',templet:"#barOption"}
            ]]
        });
        table.on('tool(listSplit)', function(obj){
            var data = obj.data;
            var iid = data.iid
            if(obj.event == 'delete'){
                layer.confirm('删除?',function () {
                    $.ajax({
                        url:'/pages/back/item/delete?'+iid,
                        type: 'POST',
                        data: {"iid":iid},
                        success:function (data) {
                            if(data == "success"){ // 表示成功
                                layer.msg('删除成功',{time:1000,icon:1,end:function () {
                                        // 登录成功之后跳转到后台首页
                                        window.location.href = "/pages/back/item/listPre";
                                    }})
                            }else if(data == "isEmpty"){ // 表示验证失败
                                layer.msg('数据不能为空或者数据格式错误',{time:1000,icon:2})
                            }else{
                                layer.msg('删除失败',{time:1000,icon:2})
                            }
                        }
                    })
                })
            }
            if(obj.event == "edit"){
                layer.open({
                    type: 1
                    ,title: false //不显示标题栏
                    ,area: ['600px','500px'],
                    content: '<div class="layui-col-md10"><form class="layui-form" style="padding-top: 20px">' +
                    ' <div class="layui-form-item">' +
                    '<label class="layui-form-label">类型描述：</label>' +
                    '<div class="layui-input-block">' +
                    '<textarea style="height: 300px" type="text" name="text" required  lay-verify="required" placeholder="请输入货物描述" autocomplete="off" class="layui-input"></textarea>' +
                    '</div>' +
                    '</div>' +
                    ' <div class="layui-form-item">' +
                    '<div class="layui-input-block">' +
                    '<input type="button" class="layui-btn layui-col-md-offset2"' +
                    'lay-submit lay-filter="formDemo" id="edit" value="提交">' +
                    '<button type="reset" class="layui-btn layui-btn-primary">重置</button>' +
                    '</div>' +
                    '</div>' +
                    '</form></div><div class="layui-col-md2">&nbsp;</div>'
                })
                $(function () {
                    $("#edit").on('click',function () {
                        var note = $('.layui-form').serialize()
                        note = decodeURIComponent(note,true).substring(5);
                        $.ajax({
                            url:'/pages/back/item/edit?'+iid,
                            type: 'POST',
                            data:{"iid":iid,"note":note},
                            success:function (data) {
                                if(data == "success"){ // 表示成功
                                    layer.msg('修改成功',{time:1000,icon:1,end:function () {
                                            // 登录成功之后跳转到后台首页
                                            window.location.href = "/pages/back/item/listPre";
                                        }})
                                }else if(data == "isEmpty"){ // 表示验证失败
                                    layer.msg('数据不能为空或者数据格式错误',{time:1000,icon:2})
                                }else{
                                    layer.msg('修改失败',{time:1000,icon:2})
                                }
                            }
                        })
                    })
                });
            }
        });
    })
})